import { Message } from 'view-design'

function validateNum(val) {
  if (!val && val !== 0) {
    return false
  }
  return true
}

function onValidateLeader(info) {
  if (info.leaderDistributionFlag) {
    if ((info.leaderSomeWay == '2' || info.leaderSomeWay == '3') && !validateNum(info.leaderSomeWayValue)) {
      Message.error('请填写组长固定提点')
      return true
    }
  }
  return false
}

function onValidateAgent(info) {
  let reg = /^\d+(\.\d{1,4}){0,1}$/
  if (!validateNum(info.defaultAgencyFee)) {
    Message.error('请填写默认虚拟代理费率')
    return true
  } else if (!reg.test(info.defaultAgencyFee)) {
    Message.error('请填写正确的虚拟代理费率，且最多保留4位小数')
    return true
  } else if (info.boVirtualSettingList.length) {
    let isError = false,
        msg = '虚拟代理费率设置有误，请检查并填写完整'
    for (let item of info.boVirtualSettingList) {
      if (!item.productIds.length) {
        isError = true
        break
      } else if (!validateNum(item.virtualAgencyFee)) {
        isError = true
        break
      } else if (!reg.test(item.virtualAgencyFee)) {
        isError = true
        msg = '请填写正确的虚拟代理费率，且最多保留4位小数'
        break
      }
    }
    if (isError) {
      Message.error(msg)
      return true
    }
  }
  return false
}

function onValidateDimission(info) {
  let isError = false
  for (let item of info.boDimissionList) {
    if (!validateNum(item.departure) || !validateNum(item.associate)) {
      isError = true
      Message.error('请填写完整离职奖金设置')
      break
    } else if (item.departure + item.associate > 100) {
      isError = true
      Message.error('离职奖金设置中离职人与交接人比例之和不能大于100')
      break
    }
  }
  if (isError) return true
  return false
}

function onValidateChannel(info) {
  if (!validateNum(info.defaultChannelSomeWayValue)) {
    Message.error('请填写默认渠道成交提点')
    return true
  } else if (info.boChannelSettingList.length) {
    let isError = false
    for (let item of info.boChannelSettingList) {
      if (!item.postIds.length || !item.productIds.length || !item.channel.length) {
        isError = true
        break
      } else if (!validateNum(item.someWayValue)) {
        isError = true
        break
      }
    }
    if (isError) {
      Message.error('渠道成交奖金设置有误，请检查并填写完整')
      return true
    }
  }
  return false
}

function onValidateRelative(info) {
  if (!validateNum(info.defaultAssociatesSomeWayValue)) {
    Message.error('请填写默认关系户成交提点')
    return true
  } else if (info.boAssociatesSettingList.length) {
    let isError = false
    for (let item of info.boAssociatesSettingList) {
      if (!item.postIds.length) {
        isError = true
        break
      } else if (!validateNum(item.someWayValue)) {
        isError = true
        break
      }
    }
    if (isError) {
      Message.error('关系户成交奖金设置有误，请检查并填写完整')
      return true
    }
  }
  return false
}

function onValidatePool(info) {
  if (info.calculateBonusPool && (!validateNum(info.viceExemplaryCase) || !validateNum(info.exemplaryCase))) {
    Message.error('请填写副专或专案奖金池点数')
    return true
  }
  return false
}

function onValidateSales(info) {
  if (info.defaultCounselorSellersSomeWay == '2' && !validateNum(info.defaultCounselorSellersSomeWayValue)) {
    Message.error('请填写默认非顾问卖房提点')
    return true
  } else if (info.boNoCounselorSellersSettingList.length) {
    let isError = false
    for (let item of info.boNoCounselorSellersSettingList) {
      if (!item.postIds.length) {
        isError = true
        break
      } else if (item.someWay == '2' && !validateNum(item.someWayValue)) {
        isError = true
        break
      }
    }
    if (isError) {
      Message.error('非顾问卖房奖金设置有误，请检查并填写完整')
      return true
    }
  }
  return false
}

function onValidateRoom(info) {
  if (info.boNoCounselorRoomSettingList.length) {
    let isError = false
    for (let item of info.boNoCounselorRoomSettingList) {
      if (!item.postIds.length) {
        isError = true
        break
      }
    }
    if (isError) {
      Message.error('非顾问房源确认规则设置有误，请检查并填写完整')
      return true
    }
  }
  return false
}

function onValidate(info) {
  if (onValidateLeader(info)) {
    return false
  } else if (onValidateAgent(info)) {
    return false
  } else if (onValidateDimission(info)) {
    return false
  } else if (onValidateChannel(info)) {
    return false
  } else if (onValidateRelative(info)) {
    return false
  } else if (onValidatePool(info)) {
    return false
  } else if (onValidateSales(info)) {
    return false
  } else if (onValidateRoom(info)) {
    return false
  }
  return true
}

export default {
  onValidate
}
